/*
 * @(#)PACKAGE_NAME 2020/3/7/0007
 *
 * Copy Right@ 人人译视界
 */


import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * <pre>
 * @author tuwei
 *
 * @version 0.9
 *
 * 修改版本: 0.9
 * 修改日期: 2020/3/7/0007
 * 修改人 :  tuwei
 * 修改说明: 初步完成
 * 复审人 ：
 * </pre>
 */
public class BIOProcessHandlerThread implements Runnable {
	Socket socket = null;
	Object service = null;
	public BIOProcessHandlerThread(Object service, Socket socket) {
		this.socket = socket;
		this.service = service;
	}

	public void run() {
		try (ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream())) {
			RpcRequest request = (RpcRequest) inputStream.readObject();
			outputStream.writeObject(invoke(request));
			outputStream.flush();
		}
		catch (Exception e) {

		}
	}

	private Object invoke(RpcRequest rpcRequest) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
		Class clazz = Class.forName(rpcRequest.getClassName());
		Method method = clazz.getMethod(rpcRequest.getMethodName(), rpcRequest.getTypes());
		return method.invoke(service, rpcRequest.getParams());
	}
}
